package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.SopCommonDiseasesMapper;
import com.zhijian.medical.entity.po.SopCommonDiseases;
import com.zhijian.medical.entity.vo.request.SopCommonDiseasesSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.SopCommonDiseasesSearchVo;
import com.zhijian.medical.entity.vo.response.SopCommonDiseasesVo;
import com.zhijian.medical.service.SopCommonDiseasesService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * SOP-签发常见病、多发病病理报告考核业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 17:02:57
 */
@Slf4j
@Service
public class SopCommonDiseasesServiceImpl implements SopCommonDiseasesService {

	@Autowired
    private SopCommonDiseasesMapper mapper;

    @Override
    public SopCommonDiseases selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SopCommonDiseasesVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(SopCommonDiseases sopCommonDiseases) {
        Date now = new Date();
        sopCommonDiseases.setCreateTime(now);
        sopCommonDiseases.setUpdateTime(now);
        return mapper.insert(sopCommonDiseases) > 0 ? sopCommonDiseases.getId() : 0L;
    }

    @Override
    public Long insertSelective(SopCommonDiseases sopCommonDiseases) {
        Date now = new Date();
        sopCommonDiseases.setCreateTime(now);
        sopCommonDiseases.setUpdateTime(now);
        return mapper.insertSelective(sopCommonDiseases) > 0 ? sopCommonDiseases.getId() : 0L;
    }

    @Override
    public boolean update(SopCommonDiseases sopCommonDiseases) {
        Date now = new Date();
        sopCommonDiseases.setUpdateTime(now);
        return mapper.updateByPrimaryKey(sopCommonDiseases) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(SopCommonDiseases record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(SopCommonDiseasesSaveOrUpdateVo saveOrUpdateVo) {
        SopCommonDiseases sopCommonDiseases = new SopCommonDiseases();
        BeanUtils.copyProperties(saveOrUpdateVo, sopCommonDiseases);
        return this.insert(sopCommonDiseases) != 0;
    }

    @Override
    public boolean modify(SopCommonDiseasesSaveOrUpdateVo saveOrUpdateVo) {
        SopCommonDiseases old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        SopCommonDiseases sopCommonDiseases = new SopCommonDiseases();
        BeanUtils.copyProperties(saveOrUpdateVo, sopCommonDiseases);
        return this.update(sopCommonDiseases);
    }

    @Override
    public SopCommonDiseasesVo selectOne(SopCommonDiseasesSearchVo sopCommonDiseasesSearchVo) {
        return mapper.selectOne(sopCommonDiseasesSearchVo);
     }

    @Override
    public Integer count(SopCommonDiseasesSearchVo sopCommonDiseasesSearchVo) {
        return mapper.count(sopCommonDiseasesSearchVo);
    }

    @Override
    public List<SopCommonDiseasesVo> selectList(SopCommonDiseasesSearchVo sopCommonDiseasesSearchVo) {
        return mapper.selectList(sopCommonDiseasesSearchVo);
    }

    @Override
    public Page<SopCommonDiseasesVo> selectPage(SopCommonDiseasesSearchVo sopCommonDiseasesSearchVo) {
        AssertUtil.check(Objects.isNull(sopCommonDiseasesSearchVo.getPageNum()) || Objects.isNull(sopCommonDiseasesSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(sopCommonDiseasesSearchVo);
        //分页
        Page<SopCommonDiseasesVo> page = new Page<>(true, count, sopCommonDiseasesSearchVo.getPageNum(), sopCommonDiseasesSearchVo.getPageSize());
        sopCommonDiseasesSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<SopCommonDiseasesVo> responseVos = this.selectList(sopCommonDiseasesSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<SopCommonDiseases> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(SopCommonDiseasesSearchVo sopCommonDiseasesSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(sopCommonDiseasesSearchVo);
    }
    
}